package io.renren.modules.doc.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import io.renren.modules.doc.entity.ChatHistoryEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
import java.util.Map;

@Mapper
public interface ChatHistoryDao extends BaseMapper<ChatHistoryEntity> {
    
    @Select("SELECT id, session_id, user_id, message, role, vector_embedding, create_time, update_time FROM sys_chat_history WHERE session_id = #{sessionId} ORDER BY create_time ASC")
    List<ChatHistoryEntity> getChatHistoryBySessionId(String sessionId);
    
    @Select("SELECT id, session_id, user_id, message, role, vector_embedding, create_time, update_time FROM sys_chat_history WHERE vector_embedding <-> #{vectorEmbedding}::vector < #{threshold} ORDER BY vector_embedding <-> #{vectorEmbedding}::vector LIMIT #{limit}")
    List<ChatHistoryEntity> findSimilarChats(Double[] vectorEmbedding, double threshold, int limit);
    
    @Select("SELECT DISTINCT ON (session_id) session_id, message as last_message, create_time as update_time " +
            "FROM sys_chat_history WHERE user_id = #{userId} " +
            "ORDER BY session_id, create_time DESC")
    List<Map<String, Object>> getUserSessions(Long userId);
}
